package net.wanderinghorse.jsonmessage;

/**
  A Response type for encapsulating error information
  in a manner conforming to JSONMessage error reporting
  conventions. This can be used as a response object
  when responder code throws, to encapsulate the exception
  as-a Response object.

  See http://code.google.com/p/jsonmessage/wiki/FrameworkConventions
  for more information.

  Though the interface specifies that toJSON() and friends
  may throw, this implementation will only throw if it is somehow
  passed completely illegal string data as its message text (e.g.
  certain binary data might cause a problem).
*/
public class ErrorResponse extends Response {
    /**
     * Default resultCode value used for this response type.
     */
    public static final int DefaultErrorCode = 666;

    /**
     * Initializes a response containing an unspecified/default
     * error message and status code.
     */
    public ErrorResponse()
    {
        super();
        this.setResultCode(DefaultErrorCode);
        this.setResultText("Unspecified Error");
    }

    /**
     * Initializes a response containing the given status
     * code and result text.
     * @param rc The resultCode value for the response.
     * If rc is 0 then DefaultErrorCode is used.
     * @param msg The resultText value for the response.
     */
    public ErrorResponse( int rc, final String msg ) {
        super();
        this.setResultCode( (rc==0) ? DefaultErrorCode : rc );
        this.setResultText(msg);
    }

    /**
     * Equivalent to ErrorResponse( rc, e.toString() ).
     *
     * @param rc The resultCode of the object. If it is 0 then DefaultErrorCode is used.
     * @param e The exception from which to extract the error message.
     */
    public ErrorResponse(int rc, final Exception e)
    {
        this( rc, e.toString() );
    }

    /**
     * Equivalent to ErrorResponse( ErrorResponse.DefaultErrorCode, e ).
     * @param e The exception from which to extract the error message.
     */
    public ErrorResponse(final Exception e)
    {
        this( DefaultErrorCode, e );
    }

    /**
     * Initializes a response containing the given status
     * code, error type code, and result text.
     *
     * @param rc The resultCode value for the response.
     * @param errType The errorType value for the response.
     * @param msg The resultText value for the response.
     */
    public ErrorResponse(int rc, int errType, String msg )
    {
        this( rc, msg );
        this.setErrorType(errType);
    }

    /**
     * Initializes this response with the given error state.
     * Equivalent to ErrorResponse( rc, et.getValue(), et.getMessage() ).
     * 
     * @param rc The result code of the response. Should
     * be non-zero, and if it is zero then some unspecified
     * non-zero value is used.
     * @param et The Response.ErrType object from which to
     * pull more specific error details.
     */
    public ErrorResponse(int rc, Response.ErrTypes et )
    {
        this( rc, et.getValue(), et.getMessage() );
    }

}
